package com.wingsofts.gankclient.ui.activity

import android.databinding.DataBindingUtil
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentPagerAdapter
import com.alibaba.android.arouter.launcher.ARouter
import com.wingsofts.gankclient.R
import com.wingsofts.gankclient.bean.FuckGoods
import com.wingsofts.gankclient.databinding.ActivityMainBinding
import com.wingsofts.gankclient.di.module.RandomModule
import com.wingsofts.gankclient.getMainComponent
import com.wingsofts.gankclient.mvp.contract.RandomContract
import com.wingsofts.gankclient.mvp.presenter.RandomPresenter
import com.wingsofts.gankclient.ui.fragment.AndroidFragment
import com.wingsofts.gankclient.ui.fragment.FragmentHolder
import com.wingsofts.gankclient.ui.fragment.GirlFragment
import com.wingsofts.gankclient.ui.fragment.IOSFragment
import kotlinx.android.synthetic.main.activity_main.*
import javax.inject.Inject

class MainActivity : BaseBindingActivity<ActivityMainBinding>(), RandomContract.View {

    @Inject
    lateinit var mPresenter: RandomPresenter

    private val mFragments = arrayListOf(
            AndroidFragment(),
            IOSFragment(),
            GirlFragment(),
            FragmentHolder()
    )

    override fun createDataBinding(savedInstanceState: Bundle?): ActivityMainBinding {
        return DataBindingUtil.setContentView(this, R.layout.activity_main)
    }

    override fun initView() {
        getMainComponent().plus(RandomModule(this)).inject(this)

        navigationView.enableAnimation(false)
        navigationView.enableShiftingMode(false)
        navigationView.enableItemShiftingMode(false)

        viewPager.adapter = object : FragmentPagerAdapter(supportFragmentManager) {
            override fun getItem(position: Int) = mFragments[position]
            override fun getCount() = mFragments.size
        }

        viewPager.offscreenPageLimit = 4

        navigationView.setupWithViewPager(viewPager)

        floatingButton.setOnClickListener {
            mPresenter.getRandom("Android")
        }
    }

    override fun onRandom(goods: FuckGoods) {
        ARouter
                .getInstance()
                .build("/activity/DetailActivity")
                .withString("detailUrl", goods.url)
                .navigation()
    }
}
